import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import './styles/common.css'
import './styles/theme.css'
import './styles/element-plus-theme.css'
import './styles/element-plus-theme-enhanced.css'
import './styles/nanchang-decorations.css'
import './styles/responsive.css'
import './styles/animations.css'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'

const app = createApp(App)

// 注册Element Plus图标
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}

app.use(store)
app.use(router)
app.use(ElementPlus)

// 全局错误处理
app.config.errorHandler = (err, instance, info) => {
  // 忽略 ResizeObserver 相关错误
  if (err?.message?.includes('ResizeObserver') || err?.message?.includes('ResizeObserver loop')) {
    return
  }
  // 忽略 Script error（通常是跨域或压缩代码的错误）
  if (err?.message === 'Script error.' || err?.message?.includes('Script error')) {
    return
  }
  console.error('Vue Error:', err, info)
}

// 全局未捕获错误处理
window.addEventListener('error', (event) => {
  // 忽略 ResizeObserver 相关错误
  if (event.message && (
    event.message.includes('ResizeObserver') ||
    event.message.includes('ResizeObserver loop')
  )) {
    event.preventDefault()
    return false
  }
  // 忽略 Script error
  if (event.message === 'Script error.' || event.message?.includes('Script error')) {
    event.preventDefault()
    return false
  }
  console.error('Global Error:', event.error, event.filename, event.lineno, event.colno)
}, true)

// Promise 未捕获错误处理
window.addEventListener('unhandledrejection', (event) => {
  // 忽略 Script error
  if (event.reason?.message === 'Script error.' || event.reason?.message?.includes('Script error')) {
    event.preventDefault()
    return false
  }
  console.error('Unhandled Promise Rejection:', event.reason)
  // 可以在这里添加错误上报逻辑
})

app.mount('#app')

